﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Nhom14_HTTTHD_BuyMe.function
{
    public class Domain
    {
        static public int AddDomain(int account, string siteURL, string description, string pageview, string price, int category)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    domain obj = new domain();
                    obj.id_account = account;
                    obj.id_type = 2;
                    obj.timestamp = DateTime.Now;
                    obj.domain_url = siteURL;
                    obj.description = description;
                    obj.page_view = pageview;
                    obj.asking_price = price;
                    obj.date_started = DateTime.Now;
                    obj.stats_available = "0";
                    obj.status = 1;

                    category obj_c = new category();
                    var result = from x in context.categories
                                 where x.id == category
                                 select x;
                    obj_c = result.First();
                    obj.categories.Add(obj_c);

                    context.domains.AddObject(obj);
                    context.SaveChanges();
                    return obj.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        static public int GetAccountID(int id)
        {
            try
            {
                websitebrokenEntities context = new websitebrokenEntities();

                var result = from x in context.websites
                             where x.id == id
                             select x;

                if (result.Count() == 0)
                    return 0;

                website web = result.First();

                return (int)web.id_account;
            }
            catch
            {
                return 0;
            }
        }

        static public int UpdateType(int id, int type)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var kq = from s in context.domains
                             where s.id == id
                             select s;

                    if (kq.Count() == 0)
                        return 0;

                    domain dmain = kq.First();

                    dmain = kq.First();
                    dmain.id_type = type;

                    context.SaveChanges();
                    return dmain.id;
                }
            }
            catch
            {
                return 0;
            }
        }


        static public int CloseDomain(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var kq = from s in context.domains
                             where s.id == id
                             select s;

                    if (kq.Count() == 0)
                        return 0;

                    domain dmain = kq.First();

                    dmain = kq.First();
                    dmain.status = 0;

                    context.SaveChanges();
                    return dmain.id;
                }
            }
            catch
            {
                return 0;
            }
        }
    }
}